﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
///VideoDao 的摘要说明
/// </summary>
public class VideoDao
{
    DBMM dbm = new DBMM();
    DataTable dt = null;
    string sql = null;
    VideoVo vv = new VideoVo();

    //添加视频
    public Exception addVideo(VideoVo vv)
    {
        sql = "insert into Video values(@Vname,@Vurl,@Vlink,@Vtype)";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Vname", vv.Vname), 
                                                 new SqlParameter("@Vurl", vv.Vurl), 
                                                 new SqlParameter("@Vlink", vv.Vlink),
                                                 new SqlParameter("@Vtype", vv.Vtype)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //查找所有视频
    public DataTable getVideo(string count)
    {
        if (count == "0")
        {
            sql = "select * from Video order by Vid desc";
        }
        else
        {
            sql = "select top " + count + " * from Video order by Vid desc";
        }
        dt = new DataTable();
        dt = dbm.GetTable(sql);
        return dt;
    }

    //查找同类型视频
    public DataTable getOtherVideo(string vid,string type)
    {
        sql = "select top 4 * from Video where Vid<>@Vid and Vtype=@Vtype order by Vid desc";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Vid", vid),
                                                 new SqlParameter("@Vtype", type)};
        dt = new DataTable();
        dt = dbm.GetTable(sql,sp);
        return dt;
    }

    //查找相关视频
    public DataTable getVideo(string name,string type)
    {
        if (type == "0")
        {
            sql = "select * from Video where Vname like @Vname order by Vid desc";
        }
        else
        {
            sql = "select * from Video where Vname like @Vname and Vtype=@Vtype order by Vid desc";
        }
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Vname", "%" + name + "%"),
                                                 new SqlParameter("@Vtype", type)};
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        return dt;
    }

    //获得单个视频信息
    public VideoVo getSingleVideo(string id)
    {
        sql = "select * from Video where Vid=@Vid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Vid", id) };
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        vv = new VideoVo();
        vv.Vid = Convert.ToInt32(dt.Rows[0][0]);
        vv.Vname = dt.Rows[0][1].ToString();
        vv.Vurl = dt.Rows[0][2].ToString();
        vv.Vlink = dt.Rows[0][3].ToString();
        vv.Vtype = dt.Rows[0][4].ToString();
        return vv;
    }

    //删除单个视频
    public Exception delVideo(string id)
    {
        sql = "delete Video where Vid=@Vid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Vid", id) };
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }
}
